package com.hongkzh.www.look.enterprise.userinfo.view.framgent;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hongkzh.www.R;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.MediaVideoBean;
import com.hongkzh.www.look.enterprise.userinfo.presenter.MediaVideoPresenter;
import com.hongkzh.www.look.enterprise.userinfo.view.adapter.MediaVideoAdapter;
import com.hongkzh.www.view.customview.MyFooter;
import com.hongkzh.www.view.fragment.BaseFragment;
import com.hongkzh.www.look.enterprise.userinfo.view.iview.MediaVideoIView;
import com.liaoinstan.springview.widget.SpringView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/***
 *  媒体中视频的Fragment
 */
public class MediaVideoFragment extends BaseFragment<MediaVideoPresenter> implements MediaVideoIView,SpringView.OnFreshListener, View.OnClickListener {


    Unbinder unbinder;

    @BindView(R.id.rv_video)
    RecyclerView mRvVideo;
    @BindView(R.id.IV_default_arrow1)
    ImageView IVDefaultArrow1;
    @BindView(R.id.layout_default)
    LinearLayout layoutDefault;
    @BindView(R.id.IV_MostScan_arrow2)
    ImageView IVMostScanArrow2;
    @BindView(R.id.layout_scan)
    LinearLayout layoutScan;
    @BindView(R.id.IV_lastUpLoad_arrow3)
    ImageView IVLastUpLoadArrow3;
    @BindView(R.id.layout_upload)
    LinearLayout layoutUpload;

    @BindView(R.id.springView_Video)
    SpringView springViewVideo;
    private MediaVideoAdapter mediaVideoAdapter;

    private boolean isDefaultUp2Low = true;  // 默认是从上到下排列
    private boolean isMostScanUp2Low = false;
    private boolean isLastUpLoadUp2Low = false;

    private String sortId = "rank"; //默认排序rank   最多浏览 view_count   最新上传 create_date
    private String sortValue = "desc";  //排序值 asc 升 desc 降

    private int PageNum=1;

    private MyFooter myFooter;

    private boolean lastPage;

    private Bundle bundle;
    private String uid;

    @Override
    protected void init() {

        bundle=getArguments();
        uid=bundle.getString("uid");
        Log.i("gaoshan","媒体视频传过来的UID===="+uid);

        setPresenter(new MediaVideoPresenter());
        mediaVideoAdapter = new MediaVideoAdapter(PageNum);

        mRvVideo.setLayoutManager(new GridLayoutManager(this.getContext(), 2, LinearLayout.VERTICAL, false));
        mRvVideo.setAdapter(mediaVideoAdapter);


        IVDefaultArrow1.setVisibility(View.VISIBLE);
        IVDefaultArrow1.setBackground(getResources().getDrawable(R.mipmap.arrow_down));
        IVMostScanArrow2.setVisibility(View.INVISIBLE);
        IVLastUpLoadArrow3.setVisibility(View.INVISIBLE);

        myFooter=new MyFooter(getActivity());
        springViewVideo.setFooter(myFooter);

        getPresenter().getMediaVideo(uid,PageNum + "", sortId, sortValue);


    }

    @Override
    protected void setListener() {
        layoutDefault.setOnClickListener(this);
        layoutScan.setOnClickListener(this);
        layoutUpload.setOnClickListener(this);
        springViewVideo.setListener(this);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_media_video;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onShowMediaVideoList(MediaVideoBean mediaVideoBean) {
        if(PageNum==1){
            mediaVideoAdapter.clearVideoList();
        }

        mediaVideoAdapter.add(mediaVideoBean);
        mediaVideoAdapter.notifyDataSetChanged();
        springViewVideo.onFinishFreshAndLoad();


    }

    @Override
    public void onLastPage(boolean lastPage) {
        this.lastPage=lastPage;
        myFooter.setLastPage(lastPage);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_default:
                // 默认rank
                isMostScanUp2Low=false;
                isLastUpLoadUp2Low=false;
                if (isDefaultUp2Low) {
                    sortId = "rank";
                    sortValue = "asc";
                    IVDefaultArrow1.setVisibility(View.VISIBLE);
                    IVDefaultArrow1.setBackground(getResources().getDrawable(R.mipmap.arrow_up));
                } else {
                    sortId = "rank";
                    sortValue = "desc";
                    IVDefaultArrow1.setVisibility(View.VISIBLE);
                    IVDefaultArrow1.setBackground(getResources().getDrawable(R.mipmap.arrow_down));
                }
                PageNum = 1;
                lastPage=false;
                myFooter.setLastPage(lastPage);
                isDefaultUp2Low = !isDefaultUp2Low;
                getPresenter().getMediaVideo(uid, PageNum + "", sortId, sortValue);
                IVMostScanArrow2.setVisibility(View.INVISIBLE);
                IVLastUpLoadArrow3.setVisibility(View.INVISIBLE);
                break;
            case R.id.layout_scan:
                // 最多浏览play_count
                isDefaultUp2Low=false;
                isLastUpLoadUp2Low=false;
                if (isMostScanUp2Low) {
                    sortId = "play_count";
                    sortValue = "asc";
                    IVMostScanArrow2.setVisibility(View.VISIBLE);
                    IVMostScanArrow2.setBackground(getResources().getDrawable(R.mipmap.arrow_up));
                } else {
                    sortId = "play_count";
                    sortValue = "desc";
                    IVMostScanArrow2.setVisibility(View.VISIBLE);
                    IVMostScanArrow2.setBackground(getResources().getDrawable(R.mipmap.arrow_down));
                }
                PageNum = 1;
                lastPage=false;
                myFooter.setLastPage(lastPage);
                isMostScanUp2Low = !isMostScanUp2Low;
                getPresenter().getMediaVideo(uid, PageNum + "", sortId, sortValue);
                IVDefaultArrow1.setVisibility(View.INVISIBLE);
                IVLastUpLoadArrow3.setVisibility(View.INVISIBLE);
                break;
            case R.id.layout_upload:
                // 最新上传
                isDefaultUp2Low=false;
                isMostScanUp2Low=false;
                if (isLastUpLoadUp2Low) {
                    sortId = "create_date";
                    sortValue = "asc";
                    IVLastUpLoadArrow3.setVisibility(View.VISIBLE);
                    IVLastUpLoadArrow3.setBackground(getResources().getDrawable(R.mipmap.arrow_up));
                } else {
                    sortId = "create_date";
                    sortValue = "desc";
                    IVLastUpLoadArrow3.setVisibility(View.VISIBLE);
                    IVLastUpLoadArrow3.setBackground(getResources().getDrawable(R.mipmap.arrow_down));
                }
                PageNum = 1;
                lastPage=false;
                myFooter.setLastPage(lastPage);
                isLastUpLoadUp2Low = !isLastUpLoadUp2Low;
                getPresenter().getMediaVideo(uid, PageNum + "", sortId, sortValue);
                IVDefaultArrow1.setVisibility(View.INVISIBLE);
                IVMostScanArrow2.setVisibility(View.INVISIBLE);
                break;
        }
    }

    @Override
    public void onError(Exception e) {

    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadmore() {
        Log.i("显示是否是最后一页======",lastPage+"");
        if(lastPage){
            springViewVideo.onFinishFreshAndLoad();
        }else{
            PageNum++;
            getPresenter().getMediaVideo(uid,PageNum + "", sortId, sortValue);
        }
    }
}
